package gajdulewiczr;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.media.opengl.GLCanvas;
import javax.media.opengl.GLCapabilities;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

import com.sun.opengl.util.FPSAnimator;

public class GUI extends JFrame implements KeyListener, WindowListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7175253116839184642L;
	private GLCanvas canvas;
	private MyGLListener listener;
	private GLCapabilities caps;
	private int DEFAULT_CANVAS_X = 800, DEFAULT_CANVAS_Y = 600;
	private FPSAnimator animator;

	private CamPosDialog cpd;

	private OptionPanel opts;

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		new GUI();
	}

	public GUI() {
		// init gl objects
		super("GK3D project 1.  h for Help.");
		caps = new GLCapabilities();
		caps.setDoubleBuffered(true);
		caps.setHardwareAccelerated(true);
		caps.setSampleBuffers(true);
		caps.setNumSamples(5);
		listener = new MyGLListener();
		canvas = new GLCanvas(caps);
		canvas.setSize(DEFAULT_CANVAS_X, DEFAULT_CANVAS_Y);
		canvas.addGLEventListener(listener);
		this.setBackground(Color.BLACK);
		// init JFrame
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.getContentPane().add(canvas, BorderLayout.CENTER);
		opts = new OptionPanel(listener, this);
		this.getContentPane().add(opts, BorderLayout.EAST);
		this.setSize(1000, 600);
		this.setResizable(false);
		this.addKeyListener(this);
		opts.addKeyListener(this);
		canvas.addKeyListener(this);
		// init Animator
		animator = new FPSAnimator(canvas, 60, true);
		animator.start();
		// display
		this.pack();
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				GUI.this.setVisible(true);
			}
		});
		for (Component comp : opts.getComponents()) {
			comp.setEnabled(false);
		}
	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void keyTyped(KeyEvent e) {
		if (e.getKeyChar() == 'w')
			listener.moveCameraForward();
		else if (e.getKeyChar() == 's')
			listener.moveCameraBackward();
		else if (e.getKeyChar() == 'a')
			listener.moveCameraLeft();
		else if (e.getKeyChar() == 'd')
			listener.moveCameraRight();
		else if (e.getKeyChar() == 'q')
			listener.lookLeft();
		else if (e.getKeyChar() == 'e')
			listener.lookRight();
		else if (e.getKeyChar() == 'r')
			listener.lookDown();
		else if (e.getKeyChar() == 'f')
			listener.lookUp();
		else if (e.getKeyChar() == 'p')
			listener.flipDebug();
		else if (e.getKeyChar() == 'o') {
			cpd = new CamPosDialog();
			cpd.addWindowListener(this);
			cpd.initDialog(new double[] { listener.xpos, listener.ypos,
					listener.zpos, listener.xrot, listener.yrot });
			cpd.setModal(true);
			cpd.setVisible(true);
		} else if (e.getKeyChar() == 'h') {
			JOptionPane.showMessageDialog(null, new JTextArea(
					"w/s - line of sight\n" + "a/d - strafe\n"
							+ "q/e - rotate left right \n"
							+ "r/f - look up/down\n"
							+ "o - set camera properties\n" + "h - help"));
		}

	}

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowClosed(WindowEvent arg0) {

	}

	@Override
	public void windowClosing(WindowEvent arg0) {

		double[] dbs = cpd.getVals();
		listener.xpos = dbs[0];
		listener.ypos = dbs[1];
		listener.zpos = dbs[2];
		listener.xrot = dbs[3];
		listener.yrot = dbs[4];

	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub

	}

}
